home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxcht.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  30.5 KB  |  1,050 lines

  1. /* Copyright (C) 1993, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxcht.c,v 1.2 2000/09/19 19:00:34 lpd Exp $ */
  20. /* Color halftone rendering for Ghostscript imaging library */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsutil.h"        /* for id generation */
  25. #include "gxarith.h"
  26. #include "gxfixed.h"
  27. #include "gxmatrix.h"
  28. #include "gxdevice.h"
  29. #include "gxcmap.h"
  30. #include "gxdcolor.h"
  31. #include "gxistate.h"
  32. #include "gzht.h"
  33.  
  34. /* Define whether to force use of the slow code, for testing. */
  35. #define USE_SLOW_CODE 0
  36.  
  37. /* Define the size of the tile buffer allocated on the stack. */
  38. #define tile_longs_LARGE 256
  39. #define tile_longs_SMALL 64
  40. #if arch_small_memory
  41. #  define tile_longs_allocated tile_longs_SMALL
  42. #  define tile_longs tile_longs_SMALL
  43. #else
  44. #  define tile_longs_allocated tile_longs_LARGE
  45. #  ifdef DEBUG
  46. #    define tile_longs\
  47.        (gs_debug_c('.') ? tile_longs_SMALL : tile_longs_LARGE)
  48. #  else
  49. #    define tile_longs tile_longs_LARGE
  50. #  endif
  51. #endif
  52.  
  53. /* Define the colored halftone device color type. */
  54. gs_private_st_ptrs1(st_dc_ht_colored, gx_device_color, "dc_ht_colored",
  55.     dc_ht_colored_enum_ptrs, dc_ht_colored_reloc_ptrs, colors.colored.c_ht);
  56. private dev_color_proc_load(gx_dc_ht_colored_load);
  57. private dev_color_proc_fill_rectangle(gx_dc_ht_colored_fill_rectangle);
  58. private dev_color_proc_equal(gx_dc_ht_colored_equal);
  59. const gx_device_color_type_t gx_dc_type_data_ht_colored = {
  60.     &st_dc_ht_colored,
  61.     gx_dc_ht_colored_load, gx_dc_ht_colored_fill_rectangle,
  62.     gx_dc_default_fill_masked, gx_dc_ht_colored_equal
  63. };
  64. #undef gx_dc_type_ht_colored
  65. const gx_device_color_type_t *const gx_dc_type_ht_colored =
  66.     &gx_dc_type_data_ht_colored;
  67. #define gx_dc_type_ht_colored (&gx_dc_type_data_ht_colored)
  68.  
  69. /* Compare two colored halftones for equality. */
  70. private bool
  71. gx_dc_ht_colored_equal(const gx_device_color * pdevc1,
  72.                const gx_device_color * pdevc2)
  73. {
  74.     uint num_comp;
  75.  
  76.     if (pdevc2->type != pdevc1->type ||
  77.     pdevc1->colors.colored.c_ht != pdevc2->colors.colored.c_ht ||
  78.     pdevc1->colors.colored.alpha != pdevc2->colors.colored.alpha ||
  79.     pdevc1->phase.x != pdevc2->phase.x ||
  80.     pdevc1->phase.y != pdevc2->phase.y
  81.     )
  82.     return false;
  83.     num_comp = pdevc1->colors.colored.c_ht->num_comp;
  84.     return
  85.     !memcmp(pdevc1->colors.colored.c_base,
  86.         pdevc2->colors.colored.c_base,
  87.         num_comp * sizeof(pdevc1->colors.colored.c_base[0])) &&
  88.     !memcmp(pdevc1->colors.colored.c_level,
  89.         pdevc2->colors.colored.c_level,
  90.         num_comp * sizeof(pdevc1->colors.colored.c_level[0]));
  91. }
  92.  
  93. /* Define an abbreviation for a heavily used value. */
  94. #define MAX_DCC GX_DEVICE_COLOR_MAX_COMPONENTS
  95.  
  96. /* Forward references. */
  97. /* Use a typedef to attempt to work around overly picky compilers. */
  98. typedef gx_color_value gx_color_value_array[MAX_DCC];
  99. typedef struct color_values_pair_s {
  100.     gx_color_value_array values[2];
  101. } color_values_pair_t;
  102. #define SET_HT_COLORS_PROC(proc)\
  103.   int proc(P7(\
  104.           color_values_pair_t *pvp,\
  105.           gx_color_index colors[1 << MAX_DCC],\
  106.           const gx_const_strip_bitmap *sbits[MAX_DCC],\
  107.           const gx_device_color *pdevc,\
  108.           gx_device *dev,\
  109.           gx_ht_cache *caches[MAX_DCC],\
  110.           int nplanes\
  111.           ))
  112.  
  113. private SET_HT_COLORS_PROC(set_ht_colors_le_4);
  114. private SET_HT_COLORS_PROC(set_cmyk_1bit_colors);
  115. private SET_HT_COLORS_PROC(set_ht_colors_gt_4);
  116.  
  117. #define SET_COLOR_HT_PROC(proc)\
  118.   void proc(P14(\
  119.             byte *dest_data, /* the output tile */\
  120.         uint dest_raster, /* ibid. */\
  121.         int px,    /* the initial phase of the output tile */\
  122.         int py,\
  123.         int w,    /* how much of the tile to set */\
  124.         int h,\
  125.         int depth,    /* depth of tile (4, 8, 16, 24, 32) */\
  126.         int special,    /* >0 means special 1-bit CMYK */\
  127.         int nplanes,\
  128.         gx_color_index plane_mask,    /* which planes are halftoned */\
  129.         gx_device *dev,        /* in case we are mapping lazily */\
  130.         const color_values_pair_t *pvp,    /* color values ditto */\
  131.         gx_color_index colors[1 << MAX_DCC],    /* the actual colors for the tile, */\
  132.                 /* actually [1 << nplanes] */\
  133.         const gx_const_strip_bitmap * sbits[MAX_DCC]    /* the bitmaps for the planes, */\
  134.                 /* actually [nplanes] */\
  135.         ))
  136.  
  137. private SET_COLOR_HT_PROC(set_color_ht_le_4);
  138. private SET_COLOR_HT_PROC(set_color_ht_gt_4);
  139.  
  140. /* Prepare to use a colored halftone, by loading the default cache. */
  141. private int
  142. gx_dc_ht_colored_load(gx_device_color * pdevc, const gs_imager_state * pis,
  143.               gx_device * ignore_dev, gs_color_select_t select)
  144. {
  145.     gx_device_halftone *pdht = pdevc->colors.colored.c_ht;
  146.     gx_ht_cache *pcache = pis->ht_cache;
  147.     gx_ht_order *porder =
  148.     (pdht->components ? &pdht->components[0].corder : &pdht->order);
  149.  
  150.     if (pcache->order.bit_data != porder->bit_data)
  151.     gx_ht_init_cache(pcache, porder);
  152.     /* Set the cache pointers in the default order. */
  153.     pdht->order.cache = porder->cache = pcache;
  154.     return 0;
  155. }
  156.  
  157. /* Fill a rectangle with a colored halftone. */
  158. /* Note that we treat this as "texture" for RasterOp. */
  159. private int
  160. gx_dc_ht_colored_fill_rectangle(const gx_device_color * pdevc,
  161.                 int x, int y, int w, int h,
  162.                 gx_device * dev, gs_logical_operation_t lop,
  163.                 const gx_rop_source_t * source)
  164. {
  165.     ulong tbits[tile_longs_allocated];
  166.     const uint tile_bytes = tile_longs * size_of(long);
  167.     gx_strip_bitmap tiles;
  168.     gx_rop_source_t no_source;
  169.     const gx_device_halftone *pdht = pdevc->colors.colored.c_ht;
  170.     int depth = dev->color_info.depth;
  171.     int nplanes = dev->color_info.num_components;
  172.     SET_HT_COLORS_PROC((*set_ht_colors)) =
  173.     (
  174. #if USE_SLOW_CODE
  175.      set_ht_colors_gt_4
  176. #else
  177.      dev_proc(dev, map_cmyk_color) == cmyk_1bit_map_cmyk_color ?
  178.        set_cmyk_1bit_colors :
  179.      nplanes < 4 ? set_ht_colors_le_4 :
  180.      set_ht_colors_gt_4
  181. #endif
  182.      );
  183.     SET_COLOR_HT_PROC((*set_color_ht)) =
  184.     (
  185. #if !USE_SLOW_CODE
  186.      !(pdevc->colors.colored.plane_mask & ~(gx_color_index)15) &&
  187.       set_ht_colors != set_ht_colors_gt_4 ?
  188.        set_color_ht_le_4 :
  189. #endif
  190.      set_color_ht_gt_4);
  191.     color_values_pair_t vp;
  192.     gx_color_index colors[1 << MAX_DCC];
  193.     const gx_const_strip_bitmap *sbits[MAX_DCC];
  194.     gx_ht_cache *caches[MAX_DCC];
  195.     int special;
  196.     int code = 0;
  197.     int raster;
  198.     uint size_x;
  199.     int dw, dh;
  200.     int lw = pdht->lcm_width, lh = pdht->lcm_height;
  201.     bool no_rop;
  202.     int i;
  203.  
  204.     if (w <= 0 || h <= 0)
  205.     return 0;
  206.     if ((w | h) >= 16) {
  207.     /* It's worth taking the trouble to check the clipping box. */
  208.     gs_fixed_rect cbox;
  209.     int t;
  210.  
  211.     dev_proc(dev, get_clipping_box)(dev, &cbox);
  212.     if ((t = fixed2int(cbox.p.x)) > x) {
  213.         if ((w += x - t) <= 0)
  214.         return 0;
  215.         x = t;
  216.     }
  217.     if ((t = fixed2int(cbox.p.y)) > y) {
  218.         if ((h += y - t) <= 0)
  219.         return 0;
  220.         y = t;
  221.     }
  222.     if ((t = fixed2int(cbox.q.x)) < x + w)
  223.         if ((w = t - x) <= 0)
  224.         return 0;
  225.     if ((t = fixed2int(cbox.q.y)) < y + h)
  226.         if ((h = t - y) <= 0)
  227.         return 0;
  228.     }
  229.     /* Colored halftone patterns are unconditionally opaque. */
  230.     lop &= ~lop_T_transparent;
  231.     if (pdht->components == 0) {
  232.     caches[0] = caches[1] = caches[2] = caches[3] = pdht->order.cache;
  233.     for (i = 4; i < nplanes; ++i)
  234.         caches[i] = pdht->order.cache;
  235.     } else {
  236.     gx_ht_order_component *pocs = pdht->components;
  237.  
  238.     caches[0] = pocs[pdht->color_indices[0]].corder.cache;
  239.     caches[1] = pocs[pdht->color_indices[1]].corder.cache;
  240.     caches[2] = pocs[pdht->color_indices[2]].corder.cache;
  241.     caches[3] = pocs[pdht->color_indices[3]].corder.cache;
  242.     for (i = 4; i < nplanes; ++i)
  243.         caches[i] = pocs[pdht->color_indices[i]].corder.cache;
  244.     }
  245.     special = set_ht_colors(&vp, colors, sbits, pdevc, dev, caches, nplanes);
  246.     no_rop = source == NULL && lop_no_S_is_T(lop);
  247.     /*
  248.      * If the LCM of the plane cell sizes is smaller than the rectangle
  249.      * being filled, compute a single tile and let tile_rectangle do the
  250.      * replication.
  251.      */
  252.     if ((w > lw || h > lh) &&
  253.     (raster = bitmap_raster(lw * depth)) <= tile_bytes / lh
  254.     ) {
  255.     /*
  256.      * The only reason we need to do fit_fill here is that if the
  257.      * device is a clipper, the caller might be counting on it to do
  258.      * all necessary clipping.  Actually, we should clip against the
  259.      * device's clipping box, not the default....
  260.      */
  261.     fit_fill(dev, x, y, w, h);
  262.     /* Check to make sure we still have a big rectangle. */
  263.     if (w > lw || h > lh) {
  264.         tiles.data = (byte *)tbits;
  265.         tiles.raster = raster;
  266.         tiles.rep_width = tiles.size.x = lw;
  267.         tiles.rep_height = tiles.size.y = lh;
  268.         tiles.id = gs_next_ids(1);
  269.         tiles.rep_shift = tiles.shift = 0;
  270.         set_color_ht((byte *)tbits, raster, 0, 0, lw, lh, depth,
  271.              special, nplanes, pdevc->colors.colored.plane_mask,
  272.              dev, &vp, colors, sbits);
  273.         if (no_rop)
  274.         return (*dev_proc(dev, strip_tile_rectangle)) (dev, &tiles,
  275.                                    x, y, w, h,
  276.                        gx_no_color_index, gx_no_color_index,
  277.                         pdevc->phase.x, pdevc->phase.y);
  278.         if (source == NULL)
  279.         set_rop_no_source(source, no_source, dev);
  280.         return (*dev_proc(dev, strip_copy_rop)) (dev, source->sdata,
  281.                    source->sourcex, source->sraster, source->id,
  282.                  (source->use_scolors ? source->scolors : NULL),
  283.                              &tiles, NULL,
  284.                              x, y, w, h,
  285.                          pdevc->phase.x, pdevc->phase.y,
  286.                                                 lop);
  287.     }
  288.     }
  289.     size_x = w * depth;
  290.     raster = bitmap_raster(size_x);
  291.     if (raster > tile_bytes) {
  292.     /*
  293.      * We can't even do an entire line at once.  See above for
  294.      * why we do the X equivalent of fit_fill here.
  295.      */
  296.     if (x < 0)
  297.         w += x, x = 0;
  298.     if (x > dev->width - w)
  299.         w = dev->width - x;
  300.     if (w <= 0)
  301.         return 0;
  302.     size_x = w * depth;
  303.     raster = bitmap_raster(size_x);
  304.     if (raster > tile_bytes) {
  305.         /* We'll have to do a partial line. */
  306.         dw = tile_bytes * 8 / depth;
  307.         size_x = dw * depth;
  308.         raster = bitmap_raster(size_x);
  309.         dh = 1;
  310.         goto fit;
  311.     }
  312.     }
  313.     /* Do as many lines as will fit. */
  314.     dw = w;
  315.     dh = tile_bytes / raster;
  316.     if (dh > h)
  317.     dh = h;
  318. fit:                /* Now the tile will definitely fit. */
  319.     if (!no_rop) {
  320.     tiles.data = (byte *)tbits;
  321.     tiles.id = gx_no_bitmap_id;
  322.     tiles.raster = raster;
  323.     tiles.rep_width = tiles.size.x = size_x / depth;
  324.     tiles.rep_shift = tiles.shift = 0;
  325.     }
  326.     while (w) {
  327.     int cy = y, ch = dh, left = h;
  328.  
  329.     for (;;) {
  330.         set_color_ht((byte *)tbits, raster,
  331.              x + pdevc->phase.x, cy + pdevc->phase.y,
  332.              dw, ch, depth, special, nplanes,
  333.              pdevc->colors.colored.plane_mask,
  334.              dev, &vp, colors, sbits);
  335.         if (no_rop) {
  336.         code = (*dev_proc(dev, copy_color))
  337.             (dev, (byte *)tbits, 0, raster, gx_no_bitmap_id,
  338.              x, cy, dw, ch);
  339.         } else {
  340.         tiles.rep_height = tiles.size.y = ch;
  341.                 if (source == NULL)
  342.                     set_rop_no_source(source, no_source, dev);
  343.         /****** WRONG - MUST ADJUST source VALUES ******/
  344.             code = (*dev_proc(dev, strip_copy_rop))
  345.             (dev, source->sdata, source->sourcex, source->sraster,
  346.              source->id,
  347.              (source->use_scolors ? source->scolors : NULL),
  348.              &tiles, NULL, x, cy, dw, ch, 0, 0, lop);
  349.         }
  350.         if (code < 0)
  351.         return code;
  352.         if (!(left -= ch))
  353.         break;
  354.         cy += ch;
  355.         if (ch > left)
  356.         ch = left;
  357.     }
  358.     if (!(w -= dw))
  359.         break;
  360.     x += dw;
  361.     if (dw > w)
  362.         dw = w;
  363.     }
  364.     return code;
  365. }
  366.  
  367. /* ---------------- Color table setup ---------------- */
  368.  
  369. /*
  370.  * We could cache this if we had a place to store it.  Even a 1-element
  371.  * cache would help performance substantially.
  372.  *   Key: device + c_base/c_level of device color
  373.  *   Value: colors table
  374.  */
  375.  
  376. /*
  377.  * We construct color halftone tiles out of multiple "planes".
  378.  * Each plane specifies halftoning for one component (R/G/B, C/M/Y/K,
  379.  * or DeviceN components).
  380.  */
  381.  
  382. private const struct {
  383.     ulong pad;            /* to get bytes aligned properly */
  384.     byte bytes[sizeof(ulong) * 8];    /* 8 is arbitrary */
  385. } ht_no_bitmap_data = { 0 };
  386. private const gx_const_strip_bitmap ht_no_bitmap = {
  387.     &ht_no_bitmap_data.bytes[0], sizeof(ulong),
  388.     {sizeof(ulong) * 8, sizeof(ht_no_bitmap_data.bytes) / sizeof(ulong)},
  389.     gx_no_bitmap_id, 1, 1, 0, 0
  390. };
  391.  
  392. /* Set the color value(s) and halftone mask for one plane. */
  393.  
  394. /* Free variables: pvp, pdc, sbits */
  395. #define SET_PLANE_COLOR_CONSTANT(i)\
  396.   BEGIN\
  397.     pvp->values[1][i] = pvp->values[0][i] = pdc->colors.colored.c_base[i];\
  398.     sbits[i] = &ht_no_bitmap;\
  399.   END
  400.  
  401. /* Free variables: pvp, pdc, sbits, caches, invert, max_color */
  402. #define SET_PLANE_COLOR(i)\
  403.   BEGIN\
  404.     uint q = pdc->colors.colored.c_base[i];\
  405.     uint r = pdc->colors.colored.c_level[i];\
  406. \
  407.     pvp->values[0][i] = fractional_color(q, max_color);\
  408.     if (r == 0)\
  409.     pvp->values[1][i] = pvp->values[0][i], sbits[i] = &ht_no_bitmap;\
  410.     else if (!invert) {\
  411.     pvp->values[1][i] = fractional_color(q + 1, max_color);\
  412.     sbits[i] = (const gx_const_strip_bitmap *)\
  413.         &gx_render_ht(caches[i], r)->tiles;\
  414.     } else {                                                        \
  415.     const gx_device_halftone *pdht = pdc->colors.colored.c_ht;  \
  416.     int nlevels =\
  417.         (pdht->components ?\
  418.          pdht->components[pdht->color_indices[i]].corder.num_levels :\
  419.          pdht->order.num_levels);\
  420. \
  421.     pvp->values[1][i] = pvp->values[0][i];                   \
  422.     pvp->values[0][i] = fractional_color(q + 1, max_color);   \
  423.     sbits[i] = (const gx_const_strip_bitmap *)\
  424.         &gx_render_ht(caches[i], nlevels - r)->tiles;    \
  425.     }\
  426.   END
  427.  
  428. /* Set up the colors and the individual plane halftone bitmaps. */
  429. private int
  430. set_ht_colors_le_4(color_values_pair_t *pvp /* only used internally */,
  431.            gx_color_index colors[1 << MAX_DCC],
  432.            const gx_const_strip_bitmap * sbits[MAX_DCC],
  433.            const gx_device_color * pdc, gx_device * dev,
  434.            gx_ht_cache * caches[MAX_DCC], int nplanes)
  435. {
  436.     gx_color_value max_color = dev->color_info.dither_colors - 1;
  437.     /*
  438.      * NB: the halftone orders are all set up for an additive color space.
  439.      *     To make these work with a subtractive device space such as CMYK,
  440.      *     it is necessary to invert both the color level and the color
  441.      *     pair. Note that if the original color was provided an additive
  442.      *     space, this will reverse (in an approximate sense) the color
  443.      *     conversion performed to express the color in the device space.
  444.      */
  445.     bool invert = dev->color_info.num_components >= 4;  /****** HACK ******/
  446.  
  447.     SET_PLANE_COLOR(0);
  448.     SET_PLANE_COLOR(1);
  449.     SET_PLANE_COLOR(2);
  450.     if (nplanes == 3) {
  451.     gx_color_value alpha = pdc->colors.colored.alpha;
  452.  
  453.     if (alpha == gx_max_color_value) {
  454. #ifndef DEBUG
  455. #  undef gx_map_rgb_color
  456.         dev_proc_map_rgb_color((*gx_map_rgb_color)) =
  457.         dev_proc(dev, map_rgb_color);
  458. #endif
  459. #define M(i)\
  460.   colors[i] = gx_map_rgb_color(dev, pvp->values[(i) & 1][0],\
  461.                    pvp->values[((i) & 2) >> 1][1],\
  462.                    pvp->values[(i) >> 2][2])
  463.         M(0); M(1); M(2); M(3); M(4); M(5); M(6); M(7);
  464. #undef M
  465.     } else {
  466. #ifndef DEBUG
  467. #  undef gx_map_rgb_alpha_color
  468.         dev_proc_map_rgb_alpha_color((*gx_map_rgb_alpha_color)) =
  469.         dev_proc(dev, map_rgb_alpha_color);
  470. #endif
  471. #define M(i)\
  472.   colors[i] = gx_map_rgb_alpha_color(dev, pvp->values[(i) & 1][0],\
  473.                      pvp->values[((i) & 2) >> 1][1],\
  474.                      pvp->values[(i) >> 2][2], alpha)
  475.         M(0); M(1); M(2); M(3); M(4); M(5); M(6); M(7);
  476. #undef M
  477.     }
  478.     } else {
  479. #ifdef DEBUG
  480. #  define do_map_cmyk_color(dev, c, m, y, k) gx_map_cmyk_color(dev, c, m, y, k)
  481. #else
  482.     dev_proc_map_cmyk_color((*do_map_cmyk_color)) =
  483.         dev_proc(dev, map_cmyk_color);
  484. #endif
  485.     SET_PLANE_COLOR(3);
  486.     if (nplanes > 4) {
  487.         /*
  488.          * Set colors for any planes beyond the 4th.  Since this code
  489.          * only handles the case of at most 4 active planes, we know
  490.          * that any further planes are constant.
  491.          */
  492.         /****** DOESN'T MAP COLORS RIGHT, DOESN'T HANDLE ALPHA ******/
  493.         int pi;
  494.  
  495.         for (pi = 4; pi < nplanes; ++pi)
  496.         SET_PLANE_COLOR_CONSTANT(pi);
  497.     }
  498.     /*
  499.      * For CMYK output, especially if the input was RGB, it's
  500.      * common for one or more of the components to be zero.
  501.      * Each zero component can cut the cost of color mapping in
  502.      * half, so it's worth doing a little checking here.
  503.      */
  504.  
  505. #define M(i)\
  506.   colors[i] = do_map_cmyk_color(dev, pvp->values[(i) & 1][0],\
  507.                 pvp->values[((i) & 2) >> 1][1],\
  508.                 pvp->values[((i) & 4) >> 2][2],\
  509.                 pvp->values[(i) >> 3][3])
  510.  
  511.       /* We know that plane_mask <= 15. */
  512.     switch ((int)pdc->colors.colored.plane_mask) {
  513.         case 15:
  514.         M(15); M(14); M(13); M(12);
  515.         M(11); M(10); M(9); M(8);
  516.         case 7:
  517.         M(7); M(6); M(5); M(4);
  518. c3:        case 3:
  519.         M(3); M(2);
  520. c1:        case 1:
  521.         M(1);
  522.         break;
  523.         case 14:
  524.         M(14); M(12); M(10); M(8);
  525.         case 6:
  526.         M(6); M(4);
  527. c2:        case 2:
  528.         M(2);
  529.         break;
  530.         case 13:
  531.         M(13); M(12); M(9); M(8);
  532.         case 5:
  533.         M(5); M(4);
  534.         goto c1;
  535.         case 12:
  536.         M(12); M(8);
  537.         case 4:
  538.         M(4);
  539.         break;
  540.         case 11:
  541.         M(11); M(10); M(9); M(8);
  542.         goto c3;
  543.         case 10:
  544.         M(10); M(8);
  545.         goto c2;
  546.         case 9:
  547.         M(9); M(8);
  548.         goto c1;
  549.         case 8:
  550.         M(8);
  551.         break;
  552.         case 0:;
  553.     }
  554.     M(0);
  555.  
  556. #undef M
  557.  
  558.     }
  559.     return 0;
  560. }
  561.  
  562. /* Set up colors using the standard 1-bit CMYK mapping. */
  563. private int
  564. set_cmyk_1bit_colors(color_values_pair_t *ignore_pvp,
  565.              gx_color_index colors[1 << MAX_DCC /*16 used*/],
  566.              const gx_const_strip_bitmap * sbits[MAX_DCC /*4 used*/],
  567.              const gx_device_color * pdc, gx_device * dev,
  568.              gx_ht_cache * caches[MAX_DCC /*4 used*/],
  569.              int nplanes /*4*/)
  570. {
  571.     const gx_device_halftone *pdht = pdc->colors.colored.c_ht;
  572.     /*
  573.      * By reversing the order of the planes, we make the pixel values
  574.      * line up with the color indices.  Then instead of a lookup, we
  575.      * can compute the pixels directly using a Boolean function.
  576.      *
  577.      * We compute each output bit
  578.      *   out[i] = (in[i] & mask1) | (~in[i] & mask0)
  579.      * We store the two masks in colors[0] and colors[1], since the
  580.      * colors array is otherwise unused in this case.  We duplicate
  581.      * the values in all the nibbles so we can do several pixels at a time.
  582.      */
  583.     bits32 mask0 = 0, mask1 = 0;
  584. #define SET_PLANE_COLOR_CMYK(i, mask)\
  585.   BEGIN\
  586.     uint r = pdc->colors.colored.c_level[i];\
  587. \
  588.     if (r == 0) {\
  589.     if (pdc->colors.colored.c_base[i])\
  590.         mask0 |= mask, mask1 |= mask;\
  591.     sbits[3 - i] = &ht_no_bitmap;\
  592.     } else {\
  593.     int nlevels =\
  594.         (pdht->components ?\
  595.          pdht->components[pdht->color_indices[i]].corder.num_levels :\
  596.          pdht->order.num_levels);\
  597. \
  598.     mask0 |= mask;\
  599.     sbits[3 - i] = (const gx_const_strip_bitmap *)\
  600.         &gx_render_ht(caches[i], nlevels - r)->tiles;\
  601.     }\
  602.   END
  603.      /* Suppress a compiler warning about signed/unsigned constants. */
  604.     SET_PLANE_COLOR_CMYK(0, /*0x88888888*/ (bits32)~0x77777777);
  605.     SET_PLANE_COLOR_CMYK(1, 0x44444444);
  606.     SET_PLANE_COLOR_CMYK(2, 0x22222222);
  607.     SET_PLANE_COLOR_CMYK(3, 0x11111111);
  608. #undef SET_PLANE_COLOR_CMYK
  609.     {
  610.     gx_ht_cache *ctemp;
  611.  
  612.     ctemp = caches[0], caches[0] = caches[3], caches[3] = ctemp;
  613.     ctemp = caches[1], caches[1] = caches[2], caches[2] = ctemp;
  614.     }
  615.     colors[0] = mask0;
  616.     colors[1] = mask1;
  617.     return 1;
  618. }
  619.  
  620. /*
  621.  * Set up colors for >4 planes.  In this case, we compute the colors
  622.  * themselves lazily.
  623.  */
  624. private int
  625. set_ht_colors_gt_4(color_values_pair_t *pvp,
  626.            gx_color_index colors[1 << MAX_DCC],
  627.            const gx_const_strip_bitmap * sbits[MAX_DCC],
  628.            const gx_device_color * pdc, gx_device * dev,
  629.            gx_ht_cache * caches[MAX_DCC], int nplanes)
  630. {
  631.     gx_color_value max_color = dev->color_info.dither_colors - 1;
  632.     /* See set_ht_color_le_4 for invert. */
  633.     bool invert = true;  /****** HACK ******/
  634.     gx_color_index plane_mask = pdc->colors.colored.plane_mask;
  635.     int i;
  636.     gx_color_index ci;
  637.  
  638.     /* Set the color values and halftone caches. */
  639.     for (i = 0; i < nplanes; ++i)
  640.     if ((plane_mask >> i) & 1)
  641.         SET_PLANE_COLOR(i);
  642.         else
  643.         SET_PLANE_COLOR_CONSTANT(i);
  644.  
  645.     /*
  646.      * Clear the color table entries that will actually be used, namely,
  647.      * those whose indices are either 0 or 1 in bits selected by plane_mask
  648.      * and 0 in all other bits.
  649.      */
  650.     ci = 0;
  651.     do {
  652.     colors[ci] = gx_no_color_index;
  653.     } while ((ci = ((ci | ~plane_mask) + 1) & plane_mask) != 0);
  654.  
  655.     return 0;
  656. }
  657.  
  658. /* ---------------- Color rendering ---------------- */
  659.  
  660. /* Define the bookkeeping structure for each plane of halftone rendering. */
  661. typedef struct tile_cursor_s {
  662.     int tile_shift;        /* X shift per copy of tile */
  663.     int xoffset;
  664.     int xshift;
  665.     uint xbytes;
  666.     int xbits;
  667.     const byte *row;
  668.     const byte *tdata;
  669.     uint raster;
  670.     const byte *data;
  671.     int bit_shift;
  672. } tile_cursor_t;
  673.  
  674. /*
  675.  * Initialize one plane cursor, including setting up for the first row
  676.  * (data and bit_shift).
  677.  */
  678. private void
  679. init_tile_cursor(int i, tile_cursor_t *ptc, const gx_const_strip_bitmap *btile,
  680.          int endx, int lasty)
  681. {
  682.     int tw = btile->size.x;
  683.     int bx = ((ptc->tile_shift = btile->shift) == 0 ? endx :
  684.           endx + lasty / btile->size.y * ptc->tile_shift) % tw;
  685.     int by = lasty % btile->size.y;
  686.  
  687.     ptc->xoffset = bx >> 3;
  688.     ptc->xshift = 8 - (bx & 7);
  689.     ptc->xbytes = (tw - 1) >> 3;
  690.     ptc->xbits = ((tw - 1) & 7) + 1;
  691.     ptc->tdata = btile->data;
  692.     ptc->raster = btile->raster;
  693.     ptc->row = ptc->tdata + by * ptc->raster;
  694.     ptc->data = ptc->row + ptc->xoffset;
  695.     ptc->bit_shift = ptc->xshift;
  696.     if_debug6('h', "[h]plane %d: size=%d,%d shift=%d bx=%d by=%dn",
  697.           i, tw, btile->size.y, btile->shift, bx, by);
  698. }
  699.  
  700. /* Step a cursor to the next row. */
  701. private void
  702. wrap_shifted_cursor(tile_cursor_t *ptc, const gx_const_strip_bitmap *psbit)
  703. {
  704.     ptc->row += ptc->raster * (psbit->size.y - 1);
  705.     if (ptc->tile_shift) {
  706.     if ((ptc->xshift += ptc->tile_shift) >= 8) {
  707.         if ((ptc->xoffset -= ptc->xshift >> 3) < 0) {
  708.         /* wrap around in X */
  709.         int bx = (ptc->xoffset << 3) + 8 - (ptc->xshift & 7) +
  710.             psbit->size.x;
  711.  
  712.         ptc->xoffset = bx >> 3;
  713.         ptc->xshift = 8 - (bx & 7);
  714.         } else
  715.         ptc->xshift &= 7;
  716.     }
  717.     }
  718. }
  719. #define STEP_ROW(c, i)\
  720.   BEGIN\
  721.     if (c.row > c.tdata)\
  722.       c.row -= c.raster;\
  723.     else {    /* wrap around to end of tile */\
  724.     wrap_shifted_cursor(&c, sbits[i]);\
  725.     }\
  726.     c.data = c.row + c.xoffset;\
  727.     c.bit_shift = c.xshift;\
  728.   END
  729.  
  730. /* Define a table for expanding 8x1 bits to 8x4. */
  731. private const bits32 expand_8x1_to_8x4[256] = {
  732. #define X16(c)\
  733.   c+0, c+1, c+0x10, c+0x11, c+0x100, c+0x101, c+0x110, c+0x111,\
  734.   c+0x1000, c+0x1001, c+0x1010, c+0x1011, c+0x1100, c+0x1101, c+0x1110, c+0x1111
  735.     X16(0x00000000), X16(0x00010000), X16(0x00100000), X16(0x00110000),
  736.     X16(0x01000000), X16(0x01010000), X16(0x01100000), X16(0x01110000),
  737.     X16(0x10000000), X16(0x10010000), X16(0x10100000), X16(0x10110000),
  738.     X16(0x11000000), X16(0x11010000), X16(0x11100000), X16(0x11110000)
  739. #undef X16
  740. };
  741.  
  742. /*
  743.  * Render the combined halftone for nplanes <= 4.
  744.  */
  745. private void
  746. set_color_ht_le_4(byte *dest_data, uint dest_raster, int px, int py,
  747.           int w, int h, int depth, int special, int nplanes,
  748.           gx_color_index plane_mask, gx_device *ignore_dev,
  749.           const color_values_pair_t *ignore_pvp,
  750.           gx_color_index colors[1 << MAX_DCC],
  751.           const gx_const_strip_bitmap * sbits[MAX_DCC])
  752. {
  753.     /*
  754.      * Note that the planes are specified in the order RGB or CMYK, but
  755.      * the indices used for the internal colors array are BGR or KYMC,
  756.      * except for the special 1-bit CMYK case.
  757.      */
  758.     int x, y;
  759.     tile_cursor_t cursor[MAX_DCC];
  760.     int dbytes = depth >> 3;
  761.     byte *dest_row =
  762.     dest_data + dest_raster * (h - 1) + (w * depth) / 8;
  763.  
  764.     if (special > 0) {
  765.     /* Planes are in reverse order. */
  766.     plane_mask =
  767.         "\000\010\004\014\002\012\006\016\001\011\005\015\003\013\007\017"[plane_mask];
  768.     }
  769.     if_debug6('h',
  770.           "[h]color_ht: x=%d y=%d w=%d h=%d plane_mask=0x%lu depth=%d\n",
  771.           px, py, w, h, (ulong)plane_mask, depth);
  772.  
  773.     /* Do one-time cursor initialization. */
  774.     {
  775.     int endx = w + px;
  776.     int lasty = h - 1 + py;
  777.  
  778.     if (plane_mask & 1)
  779.         init_tile_cursor(0, &cursor[0], sbits[0], endx, lasty);
  780.     if (plane_mask & 2)
  781.         init_tile_cursor(1, &cursor[1], sbits[1], endx, lasty);
  782.     if (plane_mask & 4)
  783.         init_tile_cursor(2, &cursor[2], sbits[2], endx, lasty);
  784.     if (plane_mask & 8)
  785.         init_tile_cursor(3, &cursor[3], sbits[3], endx, lasty);
  786.     }
  787.  
  788.     /* Now compute the actual tile. */
  789.     for (y = h; ; dest_row -= dest_raster) {
  790.     byte *dest = dest_row;
  791.  
  792.     --y;
  793.     for (x = w; x > 0;) {
  794.         bits32 indices;
  795.         int nx, i;
  796.         register uint bits;
  797.  
  798. /* Get the next byte's worth of bits.  Note that there may be */
  799. /* excess bits set beyond the 8th. */
  800. #define NEXT_BITS(c)\
  801.   BEGIN\
  802.     if (c.data > c.row) {\
  803.       bits = ((c.data[-1] << 8) | *c.data) >> c.bit_shift;\
  804.       c.data--;\
  805.     } else {\
  806.       bits = *c.data >> c.bit_shift;\
  807.       c.data += c.xbytes;\
  808.       if ((c.bit_shift -= c.xbits) < 0) {\
  809.     bits |= *c.data << -c.bit_shift;\
  810.     c.bit_shift += 8;\
  811.       } else {\
  812.     bits |= ((c.data[-1] << 8) | *c.data) >> c.bit_shift;\
  813.     c.data--;\
  814.       }\
  815.     }\
  816.   END
  817.         if (plane_mask & 1) {
  818.         NEXT_BITS(cursor[0]);
  819.         indices = expand_8x1_to_8x4[bits & 0xff];
  820.         } else
  821.         indices = 0;
  822.         if (plane_mask & 2) {
  823.         NEXT_BITS(cursor[1]);
  824.         indices |= expand_8x1_to_8x4[bits & 0xff] << 1;
  825.         }
  826.         if (plane_mask & 4) {
  827.         NEXT_BITS(cursor[2]);
  828.         indices |= expand_8x1_to_8x4[bits & 0xff] << 2;
  829.         }
  830.         if (plane_mask & 8) {
  831.         NEXT_BITS(cursor[3]);
  832.         indices |= expand_8x1_to_8x4[bits & 0xff] << 3;
  833.         }
  834. #undef NEXT_BITS
  835.         nx = min(x, 8);    /* 1 <= nx <= 8 */
  836.         x -= nx;
  837.         switch (dbytes) {
  838.         case 0:    /* 4 */
  839.             if (special > 0) {
  840.             /* Special 1-bit CMYK. */
  841.             /* Compute all the pixels at once! */
  842.             indices =
  843.                 (indices & colors[1]) | (~indices & colors[0]);
  844.             i = nx;
  845.             if ((x + nx) & 1) {
  846.                 /* First pixel is even nibble. */
  847.                 *dest = (*dest & 0xf) +
  848.                 ((indices & 0xf) << 4);
  849.                 indices >>= 4;
  850.                 --i;
  851.             }
  852.             /* Now 0 <= i <= 8. */
  853.             for (; (i -= 2) >= 0; indices >>= 8)
  854.                 *--dest = (byte)indices;
  855.             /* Check for final odd nibble. */
  856.             if (i & 1)
  857.                 *--dest = indices & 0xf;
  858.             } else {
  859.             /* Other 4-bit pixel */
  860.             i = nx;
  861.             if ((x + nx) & 1) {
  862.                 /* First pixel is even nibble. */
  863.                 *dest = (*dest & 0xf) +
  864.                 ((byte)colors[indices & 0xf] << 4);
  865.                 indices >>= 4;
  866.                 --i;
  867.             }
  868.             /* Now 0 <= i <= 8. */
  869.             for (; (i -= 2) >= 0; indices >>= 8)
  870.                 *--dest =
  871.                 (byte)colors[indices & 0xf] +
  872.                 ((byte)colors[(indices >> 4) & 0xf]
  873.                  << 4);
  874.             /* Check for final odd nibble. */
  875.             if (i & 1)
  876.                 *--dest = (byte)colors[indices & 0xf];
  877.             }
  878.             break;
  879.         case 4:    /* 32 */
  880.             for (i = nx; --i >= 0; indices >>= 4) {
  881.             bits32 tcolor = (bits32)colors[indices & 0xf];
  882.  
  883.             dest -= 4;
  884.             dest[3] = (byte)tcolor;
  885.             dest[2] = (byte)(tcolor >> 8);
  886.             tcolor >>= 16;
  887.             dest[1] = (byte)tcolor;
  888.             dest[0] = (byte)(tcolor >> 8);
  889.             }
  890.             break;
  891.         case 3:    /* 24 */
  892.             for (i = nx; --i >= 0; indices >>= 4) {
  893.             bits32 tcolor = (bits32)colors[indices & 0xf];
  894.  
  895.             dest -= 3;
  896.             dest[2] = (byte) tcolor;
  897.             dest[1] = (byte)(tcolor >> 8);
  898.             dest[0] = (byte)(tcolor >> 16);
  899.             }
  900.             break;
  901.         case 2:    /* 16 */
  902.             for (i = nx; --i >= 0; indices >>= 4) {
  903.             uint tcolor =
  904.                 (uint)colors[indices & 0xf];
  905.  
  906.             dest -= 2;
  907.             dest[1] = (byte)tcolor;
  908.             dest[0] = (byte)(tcolor >> 8);
  909.             }
  910.             break;
  911.         case 1:    /* 8 */
  912.             for (i = nx; --i >= 0; indices >>= 4)
  913.             *--dest = (byte)colors[indices & 0xf];
  914.             break;
  915.         }
  916.     }
  917.     if (y == 0)
  918.         break;
  919.  
  920.     if (plane_mask & 1)
  921.         STEP_ROW(cursor[0], 0);
  922.     if (plane_mask & 2)
  923.         STEP_ROW(cursor[1], 1);
  924.     if (plane_mask & 4)
  925.         STEP_ROW(cursor[2], 2);
  926.     if (plane_mask & 8)
  927.         STEP_ROW(cursor[3], 3);
  928.     }
  929. }
  930.  
  931. /*
  932.  * Render the combined halftone for nplanes > 4.  We haven't made any
  933.  * attempt to optimize this code.
  934.  */
  935. private void
  936. set_color_ht_gt_4(byte *dest_data, uint dest_raster, int px, int py,
  937.           int w, int h, int depth, int special, int num_planes,
  938.           gx_color_index plane_mask, gx_device *dev,
  939.           const color_values_pair_t *pvp,
  940.           gx_color_index colors[1 << MAX_DCC],
  941.           const gx_const_strip_bitmap * sbits[MAX_DCC])
  942. {
  943.     int x, y;
  944.     tile_cursor_t cursor[MAX_DCC];
  945.     int dbytes = depth >> 3;
  946.     byte *dest_row =
  947.     dest_data + dest_raster * (h - 1) + (w * depth) / 8;
  948.     int pmin, pmax;
  949.     gx_color_value cv[MAX_DCC];
  950.  
  951.     /* Compute the range of active planes. */
  952.     if (plane_mask == 0)
  953.     pmin = 0, pmax = -1;
  954.     else {
  955.     for (pmin = 0; !((plane_mask >> pmin) & 1); )
  956.         ++pmin;
  957.     for (pmax = 0; (plane_mask >> pmax) > 1; )
  958.         ++pmax;
  959.     }
  960.  
  961.     /* Do one-time cursor initialization. */
  962.     {
  963.     int endx = w + px;
  964.     int lasty = h - 1 + py;
  965.     int i;
  966.  
  967.     for (i = pmin; i <= pmax; ++i)
  968.         if ((plane_mask >> i) & 1)
  969.         init_tile_cursor(i, &cursor[i], sbits[i], endx, lasty);
  970.     }
  971.  
  972.     /* Pre-load the color value for the unchanging planes. */
  973.     {
  974.     int i;
  975.  
  976.     for (i = 0; i < pmin; ++i)
  977.         cv[i] = pvp->values[0][i];
  978.     for (i = pmax + 1; i < num_planes; ++i)
  979.         cv[i] = pvp->values[0][i];
  980.     }
  981.  
  982.     /* Now compute the actual tile. */
  983.     for (y = h; ; dest_row -= dest_raster) {
  984.     byte *dest = dest_row;
  985.     int i;
  986.  
  987.     --y;
  988.     for (x = w; x > 0;) {
  989.         gx_color_index index = 0;
  990.         gx_color_index tcolor;
  991.  
  992.         for (i = pmin; i <= pmax; ++i)
  993.         if ((plane_mask >> i) & 1) {
  994.             /* Get the next bit from an individual mask. */
  995.             tile_cursor_t *ptc = &cursor[i];
  996.             byte tile_bit;
  997.  
  998. b:            if (ptc->bit_shift < 8)
  999.             tile_bit = *ptc->data >> ptc->bit_shift++;
  1000.             else if (ptc->data > ptc->row) {
  1001.             tile_bit = *--(ptc->data);
  1002.             ptc->bit_shift = 1;
  1003.             } else {
  1004.             /* Wrap around. */
  1005.             ptc->data += ptc->xbytes;
  1006.             ptc->bit_shift = 8 - ptc->xbits;
  1007.             goto b;
  1008.             }
  1009.             index |= (gx_color_index)(tile_bit & 1) << i;
  1010.         }
  1011.         tcolor = colors[index];
  1012.         if (tcolor == gx_no_color_index) {
  1013.         /* Map the color value now. */
  1014.         int i;
  1015.  
  1016.         for (i = pmin; i <= pmax; ++i)
  1017.             cv[i] = pvp->values[(index >> i) & 1][i];
  1018.         /****** HACK -- NO WAY TO MAP GENERAL COLORS ******/
  1019.         tcolor = colors[index] =
  1020.             gx_map_cmyk_color(dev, cv[0], cv[1], cv[2], cv[3]);
  1021.         }
  1022.         --x;
  1023.         switch (dbytes) {
  1024.         case 0:    /* 4 -- might be 2, but we don't support this */
  1025.             if (x & 1) { /* odd nibble */
  1026.             *--dest = (byte)tcolor;
  1027.             } else {    /* even nibble */
  1028.             *dest = (*dest & 0xf) + ((byte)tcolor << 4);
  1029.             }
  1030.             break;
  1031.         case 4:    /* 32 */
  1032.             dest[-4] = (byte)(tcolor >> 24);
  1033.         case 3:    /* 24 */
  1034.             dest[-3] = (byte)(tcolor >> 16);
  1035.         case 2:    /* 16 */
  1036.             dest[-2] = (byte)(tcolor >> 8);
  1037.         case 1:    /* 8 */
  1038.             dest[-1] = (byte)tcolor;
  1039.             dest -= dbytes;
  1040.             break;
  1041.         }
  1042.     }
  1043.     if (y == 0)
  1044.         break;
  1045.     for (i = pmin; i <= pmax; ++i)
  1046.         if ((plane_mask >> i) & 1)
  1047.         STEP_ROW(cursor[i], i);
  1048.     }
  1049. }
  1050.